{extend name="public/base"/}
{block name="style"}
{/block}
{block name="content"}
    <div class="data-list" data-url="{:url('index')}">
        <form class="layui-form inline-form">
            <div class="pull-left">
                <div class="layui-inline">
                    <input class="layui-input" autofocus autocomplete="off" name="word" placeholder="关键词">
                </div>
                <div class="layui-inline">
                    <input class="layui-input laydate-range" autocomplete="off" name="create_time" placeholder="录入时间">
                </div>
                <div class="layui-inline">
                    <select name="cate_id" lay-search lay-filter="data-list">
                        <option value="">关键词分类</option>
                        {foreach $category as $c}
                        <option value="{$c.cate_id}">{php}echo str_repeat("├─",$c['level']);{/php}{$c.name}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="used" lay-search lay-filter="data-list">
                        <option value="">使用频率大于</option>
                        {foreach $used as $k=>$v}
                        <option value="{$v}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="hot" lay-search lay-filter="data-list">
                        <option value="">热度</option>
                        {foreach $hot as $k=>$v}
                        <option value="{$v}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="status" lay-search lay-filter="data-list">
                        <option value="">状态</option>
                        {foreach $status as $k=>$v}
                        <option value="{$k}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm layui-btn-normal search"><i class="layui-icon layui-icon-search"></i></button>
                </div>
            </div>
        </form>
        <div class="layui-row">
            <div class="layui-inline layui-btn-group">
                {if $is_show_add}
                <button class="layui-btn layui-btn-normal layui-btn-sm ajax-form" data-url="{:url('add')}"><i class="layui-icon layui-icon-add-circle"></i>新增关键词</button>
                {/if}
                <a class="layui-btn layui-btn-normal layui-btn-sm" href="/attachment/template/keyword-template.xlsx?v=1"><i class="layui-icon layui-icon-download-circle"></i>下载模板</a>
                {if $is_show_import}
                <button type="button" id="import-keyword" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-upload-drag"></i>导入关键词</button>
                {/if}
            </div>
        </div>
        <div class="data">
            <p><i class="fa fa-spinner fa-spin"></i> 加载中...</p>
        </div>
    </div>
{/block}
{block name="script"}
<script type="text/javascript">
layui.use(['layer', 'jquery', 'upload'], function()
{
    var layer = layui.layer
        , $ = layui.jquery
        , upload = layui.upload;
    // 关键词使用量
    $("body").on("click", ".used-word", function()
    {
        var $this = $(this)
            , wordId = $this.attr("item");
        if(!wordId || $this.data("is-click")) return false;
        $this.data("is-click", true);
        $.get("{:url('save')}", {"word_id": wordId, "used": 1});
    });

    // 导入关键词
    upload.render({
        elem: '#import-keyword'
        , url: '{:url("file/upload",["type"=>"document"])}'
        , accept: 'file' //普通文件
        , exts: 'xlsx|xls|csv' // 指定类型
        , field: "file"
        , before: function(obj)
        {
            layer.load(0, {shade: false});
        }
        , done: function (response, index, upload)
        {
            if (response.code != 1)
            {
                layer.closeAll('loading');
                layer.msg(response.msg, {icon: 5, time: 1500});
                return;
            }
            $.post("{:url('import')}", {"path": response.data.src}).done(function(response)
            {
                layer.closeAll('loading');
                var icon = 5, time = 5500;
                if (response.code == 1)
                {
                    icon = 6;
                    time = 1500;
                }
                layer.msg(response.msg.replace(/\n/g, "<br/>"), {icon, time}, function()
                {
                    if (response.code == 1) $(".data").closest('.data-list').getList();
                });
            });
        }
        , error: function (index, upload)
        {
            layer.msg('上传失败');
        }
    });
});
</script>
{/block}